/**
 * 
 */
import add from './sportingevents-add.js'
import del from './sportingevents-del.js'
import update from './sportingevents-update.js'

let datagrid = document.querySelector("._datagrid");
console.debug(datagrid);

let loadData = function() {
	fetch("SportingEvents-list.let").then((r) => r.json()).then((data) => {
		console.debug(data);
		datagrid.innerHTML = "";
		data.forEach((item, index) => {
			let tr = document.createElement("tr");
			tr.innerHTML = `
				<td>${item.seId}</td>
				<td>${item.seName}</td>
				<td>${item.seText}</td>
				<td class="p-2">
					<div class="btn-group btn-group-sm">
						<button type="button" class="btn btn-primary _update" title="修改">
							<i class="fa fa-edit"></i>
						</button>
						<button type="button" class="btn btn-danger _delete" title="删除">
							<i class="fa fa-remove"></i>
						</button>
					</div>
				</td>`;
			datagrid.appendChild(tr);
			tr.querySelector("._update").onclick = function() {
				update(item);
			}
			tr.querySelector("._delete").onclick = function() {
				del(item);
			}
		});
	}).catch((e) => {
		console.error(e);
	});
}
loadData();
window.addEventListener("message", function(e) {
	console.debug(e);
	if (e.data == "loadData") {
		loadData();
	}
});
add();